package com.caigp.cookbook2.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.caigp.cookbook2.activity.MenuListActivity;
import com.caigp.cookbook2.R;
import com.caigp.cookbook2.adapter.MenuLeftAdapter;
import com.caigp.cookbook2.adapter.MenuRightAdapter;
import com.caigp.cookbook2.bean.Menu;
import com.caigp.cookbook2.util.AssetsUtil;
import com.caigp.cookbook2.util.JsonUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MenuFragment extends Fragment {

    private static final String TAG = MenuFragment.class.getSimpleName();

    private View view;

    @BindView(R.id.recycler_view_left)
    RecyclerView recyclerViewLeft;

    @BindView(R.id.recycler_view_right)
    RecyclerView recyclerViewRight;

    private List<Menu> menuList;

    private MenuRightAdapter menuRightAdapter;

    private List<Menu.MenuChild> menuChildList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_menu, container, false);
        ButterKnife.bind(this, view);
        String jsonArr = AssetsUtil.getString(getContext(), "menu.json");
        menuList = JsonUtil.getList(jsonArr, Menu.class);
        menuChildList = menuList.get(0).getList();
        initRecyclerViewLeft();
        initRecyclerViewRight();
        return view;
    }

    private void initRecyclerViewRight() {
        GridLayoutManager glm = new GridLayoutManager(getContext(), 3);
        menuRightAdapter = new MenuRightAdapter(R.layout.item_menu, menuChildList);
        menuRightAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Menu.MenuChild menuChild = menuChildList.get(position);
                Intent intent = new Intent(getContext(), MenuListActivity.class);
                intent.putExtra("classid", menuChild.getClassid());
                intent.putExtra("name", menuChild.getName());
                startActivity(intent);
            }
        });
        recyclerViewRight.setLayoutManager(glm);
        recyclerViewRight.setAdapter(menuRightAdapter);
    }

    private void initRecyclerViewLeft() {
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        final MenuLeftAdapter menuLeftAdapter = new MenuLeftAdapter(R.layout.item_menu, menuList);
        menuLeftAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                menuLeftAdapter.setItemPosition(position);
                menuChildList = menuList.get(position).getList();
                menuRightAdapter.setNewData(menuChildList);
            }
        });
        recyclerViewLeft.setLayoutManager(llm);
        recyclerViewLeft.setAdapter(menuLeftAdapter);
    }
}
